Skip to content

BUG: Fix RecursionError when apply native container types as a func #61615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025

Conversation

heoh
Copy link
Contributor

@heoh heoh commented Jun 9, 2025

Description

Fixes a bug in DataFrame.apply raising RecursionError when passing func=list[int].

Cause

The existing code handled parameterized container types, but not native container types, yielding false positives. I also added a check for types.GenericAlias to handle native container types.

Reference: https://github.com/python/cpython/blob/3.12/Lib/typing.py#L1251

class _GenericAlias(_BaseGenericAlias, _root=True):
    ...
    # Objects which are instances of this class include:
    # * Parameterized container types, e.g. `Tuple[int]`, `List[int]`.
    #  * Note that native container types, e.g. `tuple`, `list`, use
    #    `types.GenericAlias` instead.

@heoh heoh closed this Jun 10, 2025
@heoh heoh reopened this Jun 10, 2025
@heoh heoh closed this Jun 10, 2025
@heoh heoh reopened this Jun 10, 2025
@@ -1298,7 +1299,7 @@ cdef bint c_is_list_like(object obj, bint allow_sets) except -1:
getattr(obj, "__iter__", None) is not None and not isinstance(obj, type)
# we do not count strings/unicode/bytes as list-like
# exclude Generic types that have __iter__
and not isinstance(obj, (str, bytes, _GenericAlias))
and not isinstance(obj, (str, bytes, _GenericAlias, GenericAlias))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need _GenericAlias and GenericAlias?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
Container classes in typing can be tested with typing._GenericAlias.
But, native container types can be tested with types.GenericAlias.
It is independent of each other.

I tested it. It was as follows:

import typing
import types
print(isinstance(typing.List[int], typing._GenericAlias))  # True
print(isinstance(typing.List[int],   types.GenericAlias))  # False
print(isinstance(       list[int], typing._GenericAlias))  # False
print(isinstance(       list[int],   types.GenericAlias))  # True

So, it is necessary to add types.GenericAlias to do # exclude Generic types that have __iter__ properly.

@mroeschke mroeschke added the Dtype Conversions Unexpected or buggy dtype conversions label Jun 16, 2025
@mroeschke mroeschke added this to the 3.0 milestone Jun 16, 2025
@mroeschke mroeschke merged commit 5909621 into pandas-dev:main Jun 16, 2025
111 of 138 checks passed
@mroeschke
Copy link
Member

Thanks @heoh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: RecursionError when apply generic alias as a func
2 participants